<?php
class Match extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('fund_model','fund');
		$this->load->model('voucher_match_model','voucher_match');
		$this->load->model('voucher_match_detail_model','voucher_match_detail');
	}
	
	function index($fund_id)
	{
		$data['fund'] = $this->fund->get_row($fund_id);
		$data['type'] = array(1=>'ด้านรับ',2=>'ด้านจ่าย',3=>'ด้านทั่วไป');
		$where = '';
		if(!empty($_GET['search']))
		{
			$where .= ' and voucher_matchs.name like \'%'.$_GET['search'].'%\'';
		}
		if(!empty($_GET['type_id']))
		{
			$where .= ' and voucher_matchs.voucher_type_id = '.$_GET['type_id'];
		}
		$data['result'] = $this->voucher_match->where('voucher_matchs.fund_id = '.$fund_id.$where)->get();
		$data['pagination'] = $this->voucher_match->pagination();
		$this->template->build('match_index',$data);
	}
	
	function form($fund_id,$id = NULL)
	{
		$data['fund'] = $this->fund->get_row($fund_id);
		$data['fund_id'] = $fund_id;
		$data['rs'] = $this->voucher_match->get_row($id);
		
		if($id)
		{
			$data['debits'] = $this->voucher_match_detail->where(' "TYPE" = \'dr\' and VOUCHER_MATCH_ID = '.$id)->get();
			$data['credits'] = $this->voucher_match_detail->where(' "TYPE" = \'cr\' and VOUCHER_MATCH_ID = '.$id)->get();
		}
		
		$this->template->append_metadata(js_validate());
		$this->template->build('match_form',$data);
	}
	
	function save()
	{
		if($_POST)
		{
			if(empty($_POST['id']))
			{
				add_log('เพิ่มจับคู่บัญชี "'.$_POST['name'].'"');
			}
			else
			{
				add_log('แก้ไขจับคู่บัญชี "'.$_POST['name'].'"');
			}
			//$_POST['user_id'] = $this->session->userdata('id');
			$voucher_id = $this->voucher_match->save($_POST);
			foreach($_POST['account_id'] as $type => $item)
			{
				foreach($item as $key => $value)
				{
					if(isset($_POST['detail_id'][$type][$key]))
					{
						$data['id'] = $_POST['detail_id'][$type][$key];
					}
					$data['account_id'] = $value;
					$data['type'] = $type;
					$data['voucher_match_id'] = $voucher_id;
					$this->voucher_match_detail->save($data);
					unset($data);
				}
			}
			set_notify('success', lang('save_data_complete'));
		}
		redirect('voucher/match/index/'.$_POST['fund_id']);
	}
	
	function delete($id)
	{
		$rs = $this->voucher_match->get_row($id);
		add_log('ลบจับคู่บัญชี "'.$rs['name'].'"');
		$this->voucher_match_detail->delete('VOUCHER_MATCH_ID',$id);
		$this->voucher_match->delete($id);
		set_notify('success', lang('delete_data_complete'));
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function detail_delete($id)
	{
		$this->db->debug = TRUE;
		$this->voucher_match_detail->delete($id);
	}
	
	function ajax($id)
	{
		$data['debits'] = $this->voucher_match_detail->where(' "TYPE" = \'dr\' and VOUCHER_MATCH_ID = '.$id)->get();
		$data['credits'] = $this->voucher_match_detail->where(' "TYPE" = \'cr\' and VOUCHER_MATCH_ID = '.$id)->get();
		$this->load->view('ajax',$data);
	}
	
	function ddl($type_id,$fund_id,$id=NULL)
	{
		$type = array(1=>'ด้านรับ',2=>'ด้านจ่าย',3=>'ด้านทั่วไป');
		echo form_dropdown('voucher_match_id',get_option('ID','NAME','VOUCHER_MATCHS','  FUND_ID = '.$fund_id.' AND VOUCHER_TYPE_ID = '.$type_id),$id,'','------------------------ '.$type[$type_id].' ------------------------');
	}
	
}
?>